//
//  AppDelegate.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/9.
//  Copyright © 2017年 刘甲奇. All rights reserved.
//

#import "AppDelegate.h"
#import "LELoginController.h"
#import "LENavigationController.h"

#import <SMS_SDK/SMSSDK.h>
#import <UMSocialCore/UMSocialCore.h>
#import <JPUSHService.h>
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import "LERemoteNotificationAlertTool.h"

@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] init];
    self.window.bounds = [UIScreen mainScreen].bounds;
    [self.window makeKeyAndVisible];
    
    //配置第三方的信息
    [self configureService:launchOptions];
    
    LELoginController *login = [[LELoginController alloc] init];
    LENavigationController *nav = [[LENavigationController alloc] initWithRootViewController:login];
    self.window.rootViewController = nav;
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    NSInteger badgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber];
    LELog(@"%s---%d---%ld",__func__,__LINE__,badgeNumber);
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}


- (void)applicationWillTerminate:(UIApplication *)application {

}

#pragma mark- 推送相关
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
 
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [JPUSHService handleRemoteNotification:userInfo];
}

#pragma mark- JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler
{
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert);
}

/**
 点击通知进入程序,就会来到这个方法

 @param center <#center description#>
 @param response <#response description#>
 @param completionHandler <#completionHandler description#>
 */
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
        //跳转到订单中心
        //审核员工
    }
    completionHandler();
}

/**
 友盟分享回调的

 @param application <#application description#>
 @param url <#url description#>
 @return <#return value description#>
 */
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}
#pragma mark- 初始化第三方
- (void)configureService:(NSDictionary *)launchOptions
{    
    //友盟
    [[UMSocialManager defaultManager] openLog:YES];
    [[UMSocialManager defaultManager] setUmSocialAppkey:USHARE_APPKEY];
    [self configUSharePlatforms];
    
    //极光推送
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)
    {
        #ifdef NSFoundationVersionNumber_iOS_9_x_Max
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
        #endif
        
    }else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    }
    [JPUSHService setupWithOption:launchOptions appKey:JPush_APP_KEY
                          channel:@"appStore"
                 apsForProduction:YES
            advertisingIdentifier:nil];
    
    //获取极光 ID
    [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        if (resCode == 0)
        {
            [[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:RegisterJPushId];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }else
        {
            LELog(@"%s---%d---注册极光失败,失败码: %d",__func__,__LINE__,resCode);
        }
    }];

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
    
}

/**
 配置友盟分享平台
 */
- (void)configUSharePlatforms
{
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1105189112" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Qzone appKey:@"1105189112" appSecret:@"BHv6qkmyhKGijCDH" redirectURL:@"http://mobile.umeng.com/social"];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"880759552" appSecret:@"5855a0de74fb33c464ea59a0bc444df7" redirectURL:@"http://mobile.umeng.com/social"];
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx8b7c087d11cde57a" appSecret:@"32a9479ef7f9224e0c7bdc31a7bd0580" redirectURL:@"http://mobile.umeng.com/social"];
    
}

/**
 接受到应用内消息

 @param notification
 */
- (void)networkDidReceiveMessage:(NSNotification *)notification
{
    NSDictionary * userInfo = [notification userInfo];
    NSString *content = [userInfo valueForKey:@"content"];
//    NSDictionary *extras = [userInfo valueForKey:@"extras"];
//    NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字段，key是自己定义的
    [[LERemoteNotificationAlertTool shareInstance] play];
    [SVProgressHUD showInfoWithStatus:content];
}
@end






